/** 
 * Project Name:sys-portal-Extjs 
 * File Name:MainCtrl.js 
 * Package Name: 
 * Author : 吴Yuan|小森森
 * Date:2018年9月3日下午1:27:12 
 * Copyright (c) 2018, 15909910367@163.com All Rights Reserved. 
 * 
 */
Ext.define('Jason.view.NewMainCtrl', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.newMainCtrl',
    init: function () {
        var me = this;
        document.title = cfg.systemname; 
        //隐藏系统默认加载框
        Ext.select('div.dimmer').hide();
        //禁用无障碍阅读
        Ext.enableAria = Ext.enableAriaButtons = Ext.enableAriaPanels = false;
        Ext.setGlyphFontFamily('FontAwesome'); //指定矢量图字体
        if (Ext.isIE8m) {
            EUtil.showMsg({
                message: '为了您更好的体验，请使用chrome浏览器或Firefox浏览器'
            });
        }
        EUtil.initCfg();//初始化系统所需设置
        if (session.get('loginState') === undefined || session.get('loginState') === false) {
            me.onNotLogin(me.getView());
        } else {//成功
            EUtil.initAccount();
            me.onLoginSuccess(me.getView());
        }
        Ext.Viewport = this.getView();
        Ext.FramePanel = Ext.Viewport.items.items[1]; // 主窗口面板
    },
    onTopMenuReady: function () {
        var me = this;
        var header = me.getView().getReferences().appHander,
            leafMenu = me.getView().getReferences().dynamicTree;
        var topMenu = header.down('customMenu');
        if (session.get('loginState') === undefined || session.get('loginState') === false) {
            me.onNotLogin(me.getView());
        } else {//成功
            EUtil.initAccount();
            me.initTopMenuData(topMenu,leafMenu);
            me.onLoginSuccess(me.getView());
        }
    },
    listen: {
        controller: {
            '#': {
                unmatchedroute: 'onRouteChange'
            }
        }
    },

    routes: {
        ':node': 'onRouteChange'
    },
    onRouteChange: function (id) {
        var self = this;
        EUtil.showLoading(I18N.pageLoading);
        if(!Ext.isEmpty(session.get('topMenuLoaded')) && session.get('topMenuLoaded') === true){
        	Ext.defer(function(){
        		self.setCurrentView(id);
        	},500);
        }else{
        	Ext.defer(function () { 
	            self.setCurrentView(id);
	        }, 800);
        }
    },
    setCurrentView: function (hashTag) {
    	console.log('执行页面跳转，跳转路由地址:',hashTag);
        var oldHashTag = hashTag;
        hashTag = (hashTag || 'protal.pages.BlankPage');
        var me = this,
            mainView = me.getView().getReferences().appBody;
        //隐藏全部子菜单
        Ext.select('div.custom-menu-sub-menus').hide();
        if (session.get('loginState') === undefined || session.get('loginState') === false) {
        	console.log('当前没有用户登录或登录已失效');
            if (hashTag === 'login') {
                hashTag = 'protal.authentication.LoginByForm';
            }
            if (hashTag === 'register') {
                hashTag = 'protal.authentication.Register';
            }
            mainView.setStyle({
                'background': 'url(resources/images/login-bg.173ae8f5.jpg)  no-repeat 50%',
                'background-size': 'cover'
            });
        } else {//成功
        	mainView.setStyle({'background': ''});
        	mainView.setStyle({'background': '#f7f8fa'});
	        var customMenu = me.getView().refs.appHander.down('customMenu'),
	            store = customMenu.getStore();
	        if(!store){
	        	CUtil.sleep(2000);
	        }
            var node = store.findNode('extendtype', hashTag);
	            hashTag = node ? node.get('view') : hashTag;
	        customMenu.setSelection(node);
            if (hashTag === 'userCenter') { /* 如果用户进入到是用户中心 */
                hashTag = 'protal.authentication.UserCenter';
            }
            if (hashTag === 'dashboard') {
                hashTag = 'protal.dashboard.Dashboard';
            }
            if (hashTag === 'main') {
                hashTag = 'NewMain';
            }
            if (hashTag === 'login') {
                hashTag = 'protal.authentication.LoginByForm';
            }
            if (hashTag === 'register') {
                hashTag = 'protal.authentication.Register';
            }
        }
        EUtil.destoryLoading();
        mainView.setCurrentViewByPath(hashTag,{
            title : Ext.isEmpty(node)?null:node.get('showname'),
            iconCls : Ext.isEmpty(node)?null:node.get('icon'),
            routeId : oldHashTag
        });
    },
    //初始化顶部按钮
    initTopMenuData: function (btnGroup,leafMenu) {
        var me = this;
        leafMenu.setStore(Ext.Viewport.getViewModel().getStore('NavigationTree'));
        leafMenu.getStore().load();
        EUtil.RS({
            url: reqUrl.sys.initMenu, method: 'GET', msg: true,
            params:{
                'authorization':session.get('access_token') || ''
            },
            message: I18N.LoadingSysMenuSource,
            callback: function (resp) {
                resp = resp.sort(CUtil.compare('order'));
                session.set('topMenuLoaded',true);
                btnGroup.setStore(Ext.create('Ext.data.TreeStore',{
                    data: resp
                }));
            }
        })
    },
    onShowSetting: function (btn) {
        var me = this;
        var topMenuConst = me.getView().down('customMenu');
        var btmLoginMsg = me.getView().down('#footerLoginMsg');
        var setting = Ext.create('Ext.menu.Menu', {
            margin: '0 0 10 0',
            items: [{
                iconCls: 'fa fa-user',
                text: '个人中心',
                handler: function (btn) {
                    Ext.create('Wys.window.PullWindow', {
                        title: '个人中心',
                        width: 550,
                        items: [{ xtype: 'usercenter' }]
                    }).show();
                }
            }, '-',{
            	iconCls: 'fax fa-cog',
                listeners:{click:me.onEnabledSettings},
            	text: '系统设置'/*,
            	menu: [
            		{iconCls:'fa-align-justify',text : '系统默认',code : 'default',listeners : {click:'onChangeTheme'}},
            		{iconCls:'fa-list-alt',text : '选项卡模式',code : 'advanced',listeners : {click:'onChangeTheme'}}
            	]*/
            },'-',{
                iconCls: 'fa fa-sign-out',
                text: '安全退出',
                handler: function (self) {
                    EUtil.showMsg({
                        option: 3,
                        title: '退出系统',
                        message: '您确认要退出系统？',
                        callback: function (btn) {
                            if (btn === 'ok') {
                                EUtil.RS({
                                    url: reqUrl.sys.logout,
                                    callback: function (result) {
                                        if (result.success) {
                                            topMenuConst.getStore().setData([]);
                                            //系统退出需要特殊处理
                                            Ext.Viewport.getViewModel().set('isTreeMenu',false);
                                            Ext.FramePanel.down('#appBody').destroy();
                                            Ext.FramePanel.add({
                                                xtype: 'card-layout-panel',
                                                region: 'center',itemId : 'appBody',
                                                reference: 'appBody',
                                                flex: 1
                                            });
                                            btmLoginMsg.setHtml('暂未登录');
                                            //显示用户信息等界面
                                            me.getView().getReferences().userProfile.hide();
                                            me.getView().getReferences().userProfile2.hide();
                                            session.clean();
                                            me.redirectTo('login');
                                        } else {
                                            EUtil.toast_(result.msg_info, 2);
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            }]
        });
        if (session.get('loginState') === undefined || session.get('loginState') === false) {
            setting.removeAll();
            setting.add({ xtype: 'container', padding: 6, html: '请登录' });
        }
        setting.showAt(btn.getX(), btn.getY() + 25);
    },
    onEnabledSettings: function () {
        Ext.create('Wys.window.PullWindow', {
            title: '系统设置',
            width: 280,
            items: [{ xtype: 'settingView' }]
        }).show();
    },
    onEnabledFullScreen: function (btn) {
        var me = this,
            fullScreen = me.getView().refs.fullScreen;
        if (Ext.isEmpty(local.get('fullScreen')) || local.get('fullScreen') === 'false') {/** 全屏  */
            local.set('fullScreen', true);
            fullScreen.setHtml('<svg t="1635915347395" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2404" width="24" height="24"><path d="M391 240.9c-0.8-6.6-8.9-9.4-13.6-4.7l-43.7 43.7L200 146.3c-3.1-3.1-8.2-3.1-11.3 0l-42.4 42.3c-3.1 3.1-3.1 8.2 0 11.3L280 333.6l-43.9 43.9c-4.7 4.7-1.9 12.8 4.7 13.6L401 410c5.1 0.6 9.5-3.7 8.9-8.9L391 240.9zM401.1 614.1L240.8 633c-6.6 0.8-9.4 8.9-4.7 13.6l43.9 43.9L146.3 824c-3.1 3.1-3.1 8.2 0 11.3l42.4 42.3c3.1 3.1 8.2 3.1 11.3 0L333.7 744l43.7 43.7c4.7 4.7 12.8 1.9 13.6-4.7l18.9-160.1c0.6-5.1-3.7-9.4-8.8-8.8zM622.9 409.9L783.2 391c6.6-0.8 9.4-8.9 4.7-13.6L744 333.6 877.7 200c3.1-3.1 3.1-8.2 0-11.3l-42.4-42.3c-3.1-3.1-8.2-3.1-11.3 0L690.3 279.9l-43.7-43.7c-4.7-4.7-12.8-1.9-13.6 4.7L614.1 401c-0.6 5.2 3.7 9.5 8.8 8.9zM744 690.4l43.9-43.9c4.7-4.7 1.9-12.8-4.7-13.6L623 614c-5.1-0.6-9.5 3.7-8.9 8.9L633 783.1c0.8 6.6 8.9 9.4 13.6 4.7l43.7-43.7L824 877.7c3.1 3.1 8.2 3.1 11.3 0l42.4-42.3c3.1-3.1 3.1-8.2 0-11.3L744 690.4z" p-id="2405" fill="#ffffff"></path></svg>');
            var docElm = document.documentElement;
            if (docElm.requestFullscreen) {
                docElm.requestFullscreen();
            } else if (docElm.mozRequestFullScreen) {
                docElm.mozRequestFullScreen();
            } else if (docElm.webkitRequestFullScreen) {
                docElm.webkitRequestFullScreen();
            } else if (docElm.msRequestFullscreen) {
                docElm.msRequestFullscreen();
            }
        } else {	/** 退出全屏*/
            local.set('fullScreen', false);
            fullScreen.setHtml('<svg t="1635911535728" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2411" width="24" height="24"><path d="M290 236.4l43.9-43.9c4.7-4.7 1.9-12.8-4.7-13.6L169 160c-5.1-0.6-9.5 3.7-8.9 8.9L179 329.1c0.8 6.6 8.9 9.4 13.6 4.7l43.7-43.7L370 423.7c3.1 3.1 8.2 3.1 11.3 0l42.4-42.3c3.1-3.1 3.1-8.2 0-11.3L290 236.4zM642.7 423.7c3.1 3.1 8.2 3.1 11.3 0l133.7-133.6 43.7 43.7c4.7 4.7 12.8 1.9 13.6-4.7L863.9 169c0.6-5.1-3.7-9.5-8.9-8.9L694.8 179c-6.6 0.8-9.4 8.9-4.7 13.6l43.9 43.9L600.3 370c-3.1 3.1-3.1 8.2 0 11.3l42.4 42.4zM845 694.9c-0.8-6.6-8.9-9.4-13.6-4.7l-43.7 43.7L654 600.3c-3.1-3.1-8.2-3.1-11.3 0l-42.4 42.3c-3.1 3.1-3.1 8.2 0 11.3L734 787.6l-43.9 43.9c-4.7 4.7-1.9 12.8 4.7 13.6L855 864c5.1 0.6 9.5-3.7 8.9-8.9L845 694.9zM381.3 600.3c-3.1-3.1-8.2-3.1-11.3 0L236.3 733.9l-43.7-43.7c-4.7-4.7-12.8-1.9-13.6 4.7L160.1 855c-0.6 5.1 3.7 9.5 8.9 8.9L329.2 845c6.6-0.8 9.4-8.9 4.7-13.6L290 787.6 423.7 654c3.1-3.1 3.1-8.2 0-11.3l-42.4-42.4z" p-id="2412" fill="#ffffff"></path></svg>');
            if (document.exitFullscreen) {
                document.exitFullscreen();
            } else if (document.mozCancelFullScreen) {
                document.mozCancelFullScreen();
            } else if (document.webkitCancelFullScreen) {
                document.webkitCancelFullScreen();
            } else if (document.msExitFullscreen) {
                document.msExitFullscreen();
            }
        }
    },
    onLoginSuccess: function (view) {
        var me = this;
        var btmLoginMsg = view.down('#footerLoginMsg');
        btmLoginMsg.setHtml(Ext.String.format('<span style="color: #333;font-weight: initial;">当前登录用户:{0},所在部门:{1},职务:{2}</span>',
            cfg.sub.accountInfo.realname,
            cfg.sub.accountInfo.deptname,
            cfg.sub.accountInfo.postText
        ));
        view.getReferences().userProfile.show();
        view.getReferences().userProfile2.show();
    },
    onNotLogin: function (view) {
        var appMainView = view.getReferences().appBody,
            btmLoginMsg = view.down('#footerLoginMsg');
        appMainView.setCurrentViewByPath('protal.authentication.LoginByForm');
        btmLoginMsg.setHtml('暂未登录');
    },
    /**
 	 * 改变系统主题
 	 */
 	onChangeTheme : function(btn){
 		var me = this;
 		localStorage.setItem("pageStyle",btn.code);
 		me._onInitPageStyle();
 	},
 	_onInitPageStyle : function(){
 		var me = this,
 			style = localStorage.getItem('pageStyle');
        Ext.FramePanel.down('#appBody').destroy();
 		switch(style){
 			case 'default':
                Ext.FramePanel.add({
                    xtype: 'card-layout-panel',
                    region: 'center',
                    itemId : 'appBody',
                    reference: 'appBody',
                    flex: 1
                });
                me.setCurrentView(Ext.History.currentToken);
 				break;
 			case 'advanced':
 				Ext.FramePanel.add({
                    xtype : 'layoutTab',region: 'center',
                    reference: 'appBody',region: 'center',
                    itemId : 'appBody',
                    flex: 1,
                    defaultTabs:[{
                        xtype : 'myDashboard',
                        routeId:'dashboard',
                        closable:false,
                        iconCls : 'fa-dashboard',
                        title:'首页'
                    }]
                });
                me.setCurrentView(Ext.History.currentToken);
 				break;
 		}
 	},
    onMenuSelectChange:function(tree, node){
        var that = this;
        try{
            if(node.get('leaf')){
                that.redirectTo(node.get('extendtype'))
            }
        }catch(e){}
    }
});